﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKTextDimension.Properties;

namespace YJKTextDimension.Forms
{
	public partial class DlgAxisDim : System.Windows.Forms.Form
	{
		public int AxisDimType
		{
			get
			{
				return this.m_nAxisDimType;
			}
		}

		public int DistAxisNumber
		{
			get
			{
				return this.m_nDistAxisNumber;
			}
		}

		public int DistBetweenDim
		{
			get
			{
				return this.m_nDistBetweenDim;
			}
		}

		public DlgAxisDim()
		{
			this.InitializeComponent();
		}

		private void DlgAxisDim_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.ReadDlgData();
			if (this.m_nDlgPosX < 0 || this.m_nDlgPosX > 3000)
			{
				this.m_nDlgPosX = 0;
			}
			if (this.m_nDlgPosY < 0 || this.m_nDlgPosY > 3000)
			{
				this.m_nDlgPosY = 0;
			}
			base.Location = new System.Drawing.Point(this.m_nDlgPosX, this.m_nDlgPosY);
			this.SetDlgItemData();
			this.ShowAxisDimTypePicture();
		}

		private void DlgAxisDim_FormClosed(object sender, FormClosedEventArgs e)
		{
			Point location = base.Location;
			this.m_nDlgPosX = location.X;
			this.m_nDlgPosY = location.Y;
			this.GetDlgItemData();
			this.SaveDlgData();
			this.CancelCommand();
		}

		private void DlgAxisDim_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelCommand()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgAxisDim.keybd_event(bVk, 0, dwFlags, 0);
			DlgAxisDim.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void radDimType1_CheckedChanged(object sender, EventArgs e)
		{
			if (this.m_nAxisDimType != 0)
			{
				this.m_nAxisDimType = 0;
				this.ShowAxisDimTypePicture();
			}
		}

		private void pictureBox2_Click(object sender, EventArgs e)
		{
			if (this.m_nAxisDimType != 0)
			{
				this.m_nAxisDimType = 0;
				this.radAxisDimType0.Checked = true;
				this.radAxisDimType1.Checked = false;
				this.radAxisDimType2.Checked = false;
				this.ShowAxisDimTypePicture();
			}
		}

		private void label3_Click(object sender, EventArgs e)
		{
			if (this.m_nAxisDimType != 0)
			{
				this.m_nAxisDimType = 0;
				this.radAxisDimType0.Checked = true;
				this.radAxisDimType1.Checked = false;
				this.radAxisDimType2.Checked = false;
				this.ShowAxisDimTypePicture();
			}
		}

		private void radDimType2_CheckedChanged(object sender, EventArgs e)
		{
			if (this.m_nAxisDimType != 1)
			{
				this.m_nAxisDimType = 1;
				this.ShowAxisDimTypePicture();
			}
		}

		private void pictureBox3_Click(object sender, EventArgs e)
		{
			if (this.m_nAxisDimType != 1)
			{
				this.m_nAxisDimType = 1;
				this.radAxisDimType0.Checked = false;
				this.radAxisDimType1.Checked = true;
				this.radAxisDimType2.Checked = false;
				this.ShowAxisDimTypePicture();
			}
		}

		private void label4_Click(object sender, EventArgs e)
		{
			if (this.m_nAxisDimType != 1)
			{
				this.m_nAxisDimType = 1;
				this.radAxisDimType0.Checked = false;
				this.radAxisDimType1.Checked = true;
				this.radAxisDimType2.Checked = false;
				this.ShowAxisDimTypePicture();
			}
		}

		private void radDimType3_CheckedChanged(object sender, EventArgs e)
		{
			if (this.m_nAxisDimType != 2)
			{
				this.m_nAxisDimType = 2;
				this.ShowAxisDimTypePicture();
			}
		}

		private void pictureBox4_Click(object sender, EventArgs e)
		{
			if (this.m_nAxisDimType != 2)
			{
				this.m_nAxisDimType = 2;
				this.radAxisDimType0.Checked = false;
				this.radAxisDimType1.Checked = false;
				this.radAxisDimType2.Checked = true;
				this.ShowAxisDimTypePicture();
			}
		}

		private void label5_Click(object sender, EventArgs e)
		{
			if (this.m_nAxisDimType != 2)
			{
				this.m_nAxisDimType = 2;
				this.radAxisDimType0.Checked = false;
				this.radAxisDimType1.Checked = false;
				this.radAxisDimType2.Checked = true;
				this.ShowAxisDimTypePicture();
			}
		}

		private void txtDistAxisNumber_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && e.KeyChar != '\r' && e.KeyChar != '\b')
			{
				e.Handled = true;
			}
		}

		private void txtDistBetweenDim_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && e.KeyChar != '\r' && e.KeyChar != '\b')
			{
				e.Handled = true;
			}
		}

		private void txtDistAxisNumber_Validating(object sender, CancelEventArgs e)
		{
		}

		private void txtDistBetweenDim_Validating(object sender, CancelEventArgs e)
		{
		}

		private bool ReadDlgData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "AxisDim.dat";
			if (!File.Exists(path))
			{
				return false;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				this.m_nDlgPosX = Convert.ToInt32(streamReader.ReadLine());
				this.m_nDlgPosY = Convert.ToInt32(streamReader.ReadLine());
				this.m_nAxisDimType = Convert.ToInt32(streamReader.ReadLine());
				this.m_nDistAxisNumber = Convert.ToInt32(streamReader.ReadLine());
				this.m_nDistBetweenDim = Convert.ToInt32(streamReader.ReadLine());
			}
			catch
			{
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		public void SaveDlgData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "AxisDim.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.m_nDlgPosX);
			streamWriter.WriteLine(this.m_nDlgPosY);
			streamWriter.WriteLine(this.m_nAxisDimType);
			streamWriter.WriteLine(this.m_nDistAxisNumber);
			streamWriter.WriteLine(this.m_nDistBetweenDim);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void SetDlgItemData()
		{
			this.radAxisDimType0.Checked = (this.m_nAxisDimType == 0);
			this.radAxisDimType1.Checked = (this.m_nAxisDimType == 1);
			this.radAxisDimType2.Checked = (this.m_nAxisDimType == 2);
			this.txtDistAxisNumber.Text = Convert.ToString(this.m_nDistAxisNumber);
			this.txtDistBetweenDim.Text = Convert.ToString(this.m_nDistBetweenDim);
		}

		public bool GetDlgItemData()
		{
			bool result;
			try
			{
				this.m_nDistAxisNumber = Convert.ToInt32(this.txtDistAxisNumber.Text);
				this.m_nDistBetweenDim = Convert.ToInt32(this.txtDistBetweenDim.Text);
				result = true;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				if (this.txtDistAxisNumber.Text == "")
				{
					this.txtDistAxisNumber.Text = this.m_nDistAxisNumber.ToString();
				}
				if (this.txtDistBetweenDim.Text == "")
				{
					this.txtDistBetweenDim.Text = this.m_nDistBetweenDim.ToString();
				}
				result = false;
			}
			return result;
		}

		private void ShowAxisDimTypePicture()
		{
			if (this.m_nAxisDimType == 0)
			{
				this.pictureBox1.Image = YJKTextDimension.Properties.Resources.AxisDimDouble;
				return;
			}
			if (this.m_nAxisDimType == 1)
			{
				this.pictureBox1.Image = YJKTextDimension.Properties.Resources.AxisDimSingle;
				return;
			}
			this.pictureBox1.Image = YJKTextDimension.Properties.Resources.AxisDimOpposite;
		}

		private int m_nDlgPosX;

		private int m_nDlgPosY;

		private int m_nAxisDimType;

		private int m_nDistAxisNumber = 700;

		private int m_nDistBetweenDim = 800;
	}
}
